home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / bigfiles < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  5.2 KB  |  185 lines

  1. #!/bin/ksh
  2. # @(#) bigfiles.ksh 1.1 97/05/21
  3. # 93/04/13 john h. dubois iii (john@armory.com)
  4. # 93/06/15 Added -d and -v options, fixed sorting so it works with -n,
  5. #          changed xargs command to avoid running l -d w/o args
  6. # 93/08/02 Added -b, -s, and -i options.
  7. # 94/06/26 Added jpg and mpg to compressed extentions
  8. # 96/01/05 5.0 port: different block size for find
  9. # 97/03/31 Do not list executable files by default.  Added X option.
  10. # 97/04/05 Use gfind if available to make ext comparisons case insensitive
  11. # 97/05/21 Added x option.  Increased defK to 10.
  12.  
  13. alias istrue="test 0 -ne"
  14. alias isfalse="test 0 -eq"
  15.  
  16. Name=${0##*/}
  17. comp_exts="Z,z,gz,hz,bz,arc,zip,zoo,gif,jpg,mpg,avi,arj,tz,tgz,thz,tbz,"
  18. bin_exts="exe,lib,com,ovl,obj,bin,sys,o,a,wav"
  19. typeset -i days=0 defK=10 verbose=0
  20. Usage=\
  21. "Usage: $Name [-abhnsv] [-i<file>] [-k<size>] [-eE<exts>] [-d<days>] [dir ..]"
  22. cut_cmd="cut -c 34-"
  23. sort_cmd="sort +4 -5 -n -r"
  24. grep_cmd=cat
  25. xmode="! -perm -100"
  26. Debug=false
  27.  
  28. while getopts :ahnvsbk:e:E:d:i:x opt; do
  29.     case $opt in
  30.     a)
  31.     comp_exts=
  32.     ;;
  33.     h) echo \
  34. "$Name: find large uncompressed files and list them sorted by size.
  35. $Usage
  36. The named directories and all subdirectories of them are searched for files
  37. larger than ${defK}K.  Any such ordinary non-executable files that do not have
  38. an extension indicating that they are compressed are listed.  The compression
  39. extensions are:" {$comp_exts} "
  40. If the 'gfind' utility is available, extensions will be matched regardless of
  41. their case.  If no directory names are given, the current directory is used.
  42. Options:
  43. -h: Print this help.
  44. -n: Print filenames only.  By default the size, date, and name are printed.
  45. -e<ext[,ext...]>: Do not list files with any of the given extensions.
  46.    The given list replaces the internal list.  Extensions should be
  47.    separated by commas and should not be preceded by a '.'  Example: -ez,Z,gz
  48. -E<ext[,ext...]>: Like -e, except that the given extensions are added to
  49.    the internal list instead of replacing it.
  50. -b: Add the following binary file extensions to the default list of ignored
  51.    extensions:" {$bin_exts} "
  52. -i<file>: Ignore the files whose names are given in <file>, one per line.
  53.    The filenames must be given with the same path as $Name would print.
  54. -a: List all big files regardless of extension.
  55. -X: Check all regular files.  Normally files executable by the invoking user
  56.     are skipped.
  57. -d<days>: List only files that have not been accessed in at least <days> days.
  58.    This changes the default for <size> to 0; that is, all files at least <days>
  59.    old will be listed, regardless of size.  The size can still be set with -k.
  60. -k<size>: List only files larger than <size> kilobytes.
  61. -s: Sort by filename instead of size.
  62. -v: Print the command issued to find files."
  63.        exit 0;;
  64.     X)
  65.     xmode=;;
  66.     x)
  67.     Debug=true;;
  68.     n)
  69.     cut_cmd="awk '{ print \$9; }'"
  70.     ;;
  71.     k) 
  72.     typeset -i K=$OPTARG || exit 1
  73.     ;;
  74.     d) 
  75.     days=$OPTARG || exit 1
  76.     defK=0
  77.     ;;
  78.     v)
  79.     verbose=1
  80.     ;;
  81.     e) 
  82.     comp_exts=$OPTARG
  83.     ;;
  84.     E) 
  85.     comp_exts="$comp_exts,$OPTARG"
  86.     ;;
  87.     b)
  88.     comp_exts="$comp_exts,$bin_exts"
  89.     ;;
  90.     s)
  91.     sort_cmd="sort +8"
  92.     ;;
  93.     i)
  94.     grep_cmd="fgrep -xvf $OPTARG"
  95.     ;;
  96.     +?) 
  97.     echo "$Name: options should not be preceded by a '+'."
  98.     exit 1
  99.     ;;
  100.     :) 
  101.     print -u2 "$Name: Option '$OPTARG' requires a value.  Use -h for help."
  102.     exit 1
  103.     ;;
  104.     ?) 
  105.     echo "$Name: $OPTARG: bad option.  Use -h for help."
  106.     exit 1
  107.     ;;
  108.     esac
  109. done
  110.  
  111. # Like the ! that later ksh have
  112. function not { "$@"; return $((!$?)); }
  113.  
  114. # Sets _OSRelease to the entire OS release (e.g. 3.2v5.0.0}
  115. # Sets _OSVersion to the version part (e.g. 5.0.0)
  116. # Returns the major part of the version (e.g. 5)
  117. # If an OSVersion value is given as an arg, returns 0 if the system OS version
  118. # is lexicographically less than it; 1 if they are equal; 2 if the system OS
  119. # version is greater.
  120. function OSVersion {
  121.     typeset arg=$1
  122.     if [ -z "$_OSRelease" ]; then
  123.     # Name of release field is different in different langs
  124.     set -- $(LANG=english_us.ascii uname -X)
  125.     while [ "$1" != Release -a $# -ge 3 ]; do
  126.         shift 3
  127.     done
  128.     [ $# -lt 3 ] && return 1
  129.     _OSRelease=$3
  130.     _OSVersion=${3##*v}
  131.     fi
  132.     [ -z "$arg" ] && return ${_OSVersion%%.*}
  133.     [[ "$_OSVersion" > "$arg" ]] && return 2
  134.     [[ "$_OSVersion" < "$arg" ]] && return 0
  135.     return 1
  136. }
  137.  
  138. # Increase the liklihood that we can find gfind
  139. [[ "$0" = */* ]] && PATH=$PATH:${0%/*} || PATH=$PATH:.
  140.  
  141. if type gfind > /dev/null; then
  142.     find=gfind
  143.     nam=iname
  144.     $Debug && print -ru2 -- "Found gfind"
  145. else
  146.     find=find
  147.     nam=name
  148. fi
  149.  
  150. [ -z "$K" ] && typeset -i K=$defK
  151. # In 5.0.0 the units for find changed to 1/2 K blocks
  152. if not OSVersion 5.0.0 || [ $find = gfind ]; then
  153.     K=$((K*2))
  154. fi
  155.  
  156. # remove args that were options
  157. let OPTIND=OPTIND-1
  158. shift $OPTIND
  159.  
  160. [ $# -eq 0 ] && set -- .
  161.  
  162. set +o noglob
  163.  
  164. ExtPred=
  165. if [ -n "$comp_exts" ]; then
  166.     ExtPred='( '`eval echo "-$nam \*."{$comp_exts}" -o"`' -name "" )'
  167. fi
  168.  
  169. set -o noglob
  170.  
  171. # Use val & +val instead of +(val-1) so that they will work if val is 0,
  172. # w/o having to remove expression for special case 0.
  173. istrue verbose && set -x
  174.  
  175. for dir
  176. do
  177.     $find "$dir" -size +$K -type f $xmode \
  178.     \( -atime $days -o -atime +$days \)\
  179.     \( -mtime $days -o -mtime +$days \) ! $ExtPred -print |
  180.     $grep_cmd | 
  181.     ( xargs sh -c '[ $# -gt 0 ] && l -d "$@"' ) |
  182.     $sort_cmd |
  183.     eval $cut_cmd
  184. done
  185.